﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Windows.Forms;
using Dll;
using Dll.Model;
using System.Threading;
using System.Security.Cryptography;
using System.Configuration;
using WindowsFormsApplication1.HttpServer;
using System.Web.Script.Serialization;

namespace WindowsFormsApplication1
{
    public partial class Shufatiyan : Form
    {

        public Shufatiyan()
        {
            InitializeComponent();
        }
        //Enrollment em = new Enrollment();
        private void Shufatiyan_Load(object sender, EventArgs e)
        {
            String appid = Cryptography.GetUUid();
            bool djkgdsf = false;
            Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
            try
            {
                String identifier = config.AppSettings.Settings["Identifier"].Value;

                String entity = System.IO.File.ReadAllText(Application.StartupPath + "\\csdra.exe");
                String result = Cryptography.AES_decrypt(identifier, ref appid);
                 entity = Cryptography.AES_decrypt(entity, ref appid);
                Identifier identifierO = new JavaScriptSerializer().Deserialize<Identifier>(result);
                Identifier entityO = new JavaScriptSerializer().Deserialize<Identifier>(entity);
                if (identifierO.appId.Equals(appid) && identifierO.appId.Equals(entityO.appId) && identifierO.registeredTime.Equals(entityO.registeredTime) && identifierO.securityCode.Equals(entityO.securityCode) && identifierO.endTime.Equals(entityO.endTime) && DateTime.Compare(entityO.endTime, DateTime.Now) > 0 && DateTime.Compare(entityO.preTime, DateTime.Now) < 0)
                {
                    djkgdsf = true;
                    entityO.preTime = DateTime.Now;
                    System.IO.File.WriteAllText(Application.StartupPath + "\\csdra.exe", Cryptography.AES_encrypt(new JavaScriptSerializer().Serialize(entityO), appid));                    
                    HttpServer.HttpServer http = new HttpServer.HttpServer(@"D:\resource", Application.StartupPath, 29107);
                    new Thread(http.start).Start();
                }
            }
            catch (Exception ex)
            {
                Registered r = new Registered(appid);
                r.Show();
                r.FormClosed += new FormClosedEventHandler(r_FormClosed);
                Console.WriteLine(ex.Message);
            }




            //DateTime now = DateTime.Now;
            //Identifier i = new Identifier();
            //i.appId = appid;
            //i.endTime = now.AddYears(1);
            //i.preTime = now;
            //i.registeredTime = now;
            //i.securityCode = "flkajsdfkljasdkjglkjsdkflj";
            //JavaScriptSerializer js = new JavaScriptSerializer();
            //String result = js.Serialize(i);
            ////Console.WriteLine(result);



            //String resytl = Cryptography.AES_encrypt(result, EncodingAESKey, appid);
            //Console.WriteLine(resytl);
            //String result = Cryptography.AES_decrypt(resytl, EncodingAESKey, ref appid);
            //Console.WriteLine(result);
            //HttpService.HttpServer http = new HttpService.HttpServer(@"D:\resource.db", Application.StartupPath + "\\files",29107);
            //new Thread(http.start).Start();
            // http.start();

        }

        void r_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.OnLoad(null);
        }
    }
}
